﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xfs;

namespace XfsClient
{
    [XfsObjectSystem]
    public class XfsConsoleUserComponentAwakeSystem : XfsAwakeSystem<XfsConsoleUserLoginComponent>
    {
        public override void Awake(XfsConsoleUserLoginComponent self)
        {

        }

    }

    [XfsObjectSystem]
    public class XfsConsoleUserComponentStartSystem : XfsStartSystem<XfsConsoleUserLoginComponent>
    {
        public override async void Start(XfsConsoleUserLoginComponent self)
        {
            await XfsTimerComponent.Instance.WaitAsync(2000);

            this.TestUserLogin(self).Coroutine();
        }

        private async XfsVoid TestUserLogin(XfsConsoleUserLoginComponent self)
        {
            Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

            if (sessions.Count > 0)
            {
                XfsSession session = sessions.Values.ToList()[0];
                if (!session.IsRunning) return;

                C4S_User resqustC = new C4S_User() { Username = self.User.Username, Password = self.User.Password };

                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 40. User." + resqustC.Username + "/" + resqustC.Password + " 开始登录...");

                S4C_User? responseC = await session.Call(resqustC) as S4C_User;

                if (responseC != null)
                {
                    XfsUser? user = XfsComponentFactory.Create<XfsUser>();

                    user.Username = responseC.Username;
                    user.Password = responseC.Password;
                    user.Id = responseC.Userid;

                    if (!XfsGame.XfsSence.GetComponent<XfsUserComponent>().Users.TryGetValue(responseC.Username, out XfsUser user1))
                    {
                        XfsGame.XfsSence.GetComponent<XfsUserComponent>().Users.Add(responseC.Username, user);
                    }

                    ///从服务端发回来的信息
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 60. User." + responseC.Userid + "/" + responseC.Username + "/" + responseC.Password + " 登录成功...");
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 61. Users." + XfsGame.XfsSence.GetComponent<XfsUserComponent>().Users.Count);
                }
            }


        }
    }

    [XfsObjectSystem]
    public class XfsConsoleUserComponentUpdateSystem : XfsUpdateSystem<XfsConsoleUserLoginComponent>
    {
        public override void Update(XfsConsoleUserLoginComponent self)
        {
            ////this.TestUserLoginCall(self).Coroutine();


        }

        int time = 0;
        int restime = 2000;
        bool isLogin = false;

        private async XfsVoid TestUserLoginCall(XfsConsoleUserLoginComponent self)
        {
            time += 1;

            if (time > restime && !isLogin)
            {
                time = 0;

                Dictionary<long, XfsSession> sessions = XfsGame.XfsSence.GetComponent<XfsNetOuterComponent>().Sessions;

                if (sessions.Count > 0)
                {
                    XfsSession session = sessions.Values.ToList()[0];

                    //C4S_User resqustC = new C4S_User() { Username = "Tumo1", Password = "1234" };

                    C4S_User resqustC = new C4S_User() { Username = self.User.Username, Password = self.User.Password };

                    if (!session.IsRunning) return;

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 20. User." + resqustC.Username + " 开始登录...");

                    S4C_User? responseC = await session.Call(resqustC) as S4C_User;

                    if (responseC != null)
                    {
                        ///从服务端发回来的信息

                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 26. User." + responseC.Username + " 登录成功...");

                        XfsUser? user = XfsComponentFactory.Create<XfsUser>();

                        user.Username = responseC.Username;
                        user.Password = responseC.Password;
                        user.Id = responseC.Userid;

                        XfsGame.XfsSence.GetComponent<XfsUserComponent>().Users.Add(responseC.Username, user);

                        isLogin = true;
                    }

                }

            }
        }


    }


}
